<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>compare</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Strings</a> &gt; <a href=
    "compare.html">compare</a>
  </div>

  <div class="name-format">
    compare
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string&gt;
  int compare( const string&amp; str );
  int compare( const char* str );
  int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string&amp; str );
  int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string&amp; str, <strong>size_type</strong> index2,
  <strong>size_type</strong> length2 );
  int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const char* str, <strong>size_type</strong> length2 );
</pre>

  <p>The compare() function either compares <em>str</em> to the current
  string in a variety of ways, returning</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Return Value</th>

      <th class="code-table-th">Case</th>
    </tr>

    <tr>
      <td class="code-table-td">less than zero</td>

      <td class="code-table-td">this &lt; str</td>
    </tr>

    <tr>
      <td class="code-table-td">zero</td>

      <td class="code-table-td">this == str</td>
    </tr>

    <tr>
      <td class="code-table-td">greater than zero</td>

      <td class="code-table-td">this &gt; str</td>
    </tr>
  </table>

  <p>The various functions either:</p>

  <ul>
    <li>compare <em>str</em> to the current string,</li>

    <li>compare <em>str</em> to a substring of the current string,
    starting at <em>index</em> for <em>length</em> characters,</li>

    <li>compare a substring of <em>str</em> to a substring of the
    current string, where <em>index2</em> and <em>length2</em> refer to
    <em>str</em> and <em>index</em> and <em>length</em> refer to the
    current string,</li>

    <li>or compare a substring of <em>str</em> to a substring of the
    current string, where the substring of <em>str</em> begins at zero
    and is <em>length2</em> characters long, and the substring of the
    current string begins at <em>index</em> and is <em>length</em>
    characters long.</li>
  </ul>

  <p>For example, the following code uses compare() to compare four
  strings with eachother:</p>
  <pre class="example-code">
 string names[] = {&quot;Homer&quot;, &quot;Marge&quot;, &quot;3-eyed fish&quot;, &quot;inanimate carbon rod&quot;};            

 for( int i = 0; i &lt; 4; i++ ) {
   for( int j = 0; j &lt; 4; j++ ) {
     cout &lt;&lt; names[i].compare( names[j] ) &lt;&lt; &quot; &quot;;
   }
   cout &lt;&lt; endl;
 }              
</pre>

  <p>Data from the above code was used to generate this table, which
  shows how the various strings compare to eachother:</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th"></th>

      <th class="code-table-th">Homer</th>

      <th class="code-table-th">Marge</th>

      <th class="code-table-th">3-eyed fish</th>

      <th class="code-table-th">inanimate carbon rod</th>
    </tr>

    <tr>
      <td class="code-table-td">&quot;Homer&quot;.compare( x )</td>

      <td class="code-table-td">0</td>

      <td class="code-table-td">-1</td>

      <td class="code-table-td">1</td>

      <td class="code-table-td">-1</td>
    </tr>

    <tr>
      <td class="code-table-td">&quot;Marge&quot;.compare( x )</td>

      <td class="code-table-td">1</td>

      <td class="code-table-td">0</td>

      <td class="code-table-td">1</td>

      <td class="code-table-td">-1</td>
    </tr>

    <tr>
      <td class="code-table-td">&quot;3-eyed fish&quot;.compare( x
      )</td>

      <td class="code-table-td">-1</td>

      <td class="code-table-td">-1</td>

      <td class="code-table-td">0</td>

      <td class="code-table-td">-1</td>
    </tr>

    <tr>
      <td class="code-table-td">&quot;inanimate carbon
      rod&quot;.compare( x )</td>

      <td class="code-table-td">1</td>

      <td class="code-table-td">1</td>

      <td class="code-table-td">1</td>

      <td class="code-table-td">0</td>
    </tr>
  </table>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="string_operators.html">String operators</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
